<template>
  <div>
    <el-card class="!border-none mb-4" shadow="never">
      <el-form class="mb-[-16px]" :model="queryParams" inline>

        <el-form-item label="院区/楼栋">
          <el-cascader
              :options="searchLists.cb_lists"
              clearable
              v-model="queryParams.cb_ids"
              filterable
              :props="props1"
          />
        </el-form-item>

        <el-form-item label="房间名" prop="name">
          <el-input class="w-[280px]" v-model="queryParams.name" clearable placeholder="请输入房间名"/>
        </el-form-item>
        <el-form-item label="地址" prop="name">
          <el-input class="w-[280px]" v-model="queryParams.address" clearable placeholder="请输入地址"/>
        </el-form-item>

        <el-form-item label="房间类型" prop="type">
          <el-select class="w-[280px]" v-model="queryParams.type" clearable placeholder="请选择房间类型">
            <el-option label="全部" value=""></el-option>
            <el-option
                v-for="(item, index) in searchLists.type_list"
                :key="index"
                :label="item"
                :value="index"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="科室" prop="name">
          <el-input class="w-[280px]" v-model="queryParams.room_department" clearable placeholder="请输入科室"/>
        </el-form-item>

        <el-form-item label="电梯费类型" prop="lift_type">
          <el-select class="w-[280px]" v-model="queryParams.lift_type" clearable placeholder="请选择电梯费类型">
            <el-option label="全部" value=""></el-option>
            <el-option v-for="(item, index) in searchLists.lift_list" :key="index" :label="item" :value="index"/>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <el-button v-perms="['room.room_hospital/add']" type="primary" @click="handleAdd">
        <template #icon>
          <icon name="el-icon-Plus"/>
        </template>
        新增
      </el-button>
      <el-button
          v-perms="['room.room_hospital/delete']"
          :disabled="!selectData.length"
          @click="handleDelete(selectData)"
      >
        删除
      </el-button>

      <el-button type="warning" @click="updateFee">
        <template #icon>
          <icon name="el-icon-Plus"/>
        </template>
        更新电梯费和二次加压费
      </el-button>

      <el-button type="warning" @click="monthInit">
        <template #icon>
          <icon name="el-icon-Plus"/>
        </template>
        月初始化
      </el-button>

      <div class="mt-4">
        <el-table :data="pager.lists" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"/>
          <el-table-column label="序号" width="90" prop="sort" show-overflow-tooltip/>

          <el-table-column label="房间信息" width="250">
            <template #default="{ row }">
              <!--title="Title"-->
              <el-popover placement="top-start" :width="300" trigger="hover">
                <template #reference>
                  <el-text>
                    {{ row.name }} <br/>
                    <view v-if="row.campus">{{ row.campus.name }}-</view>
                    <view v-if="row.building">-{{ row.building.name }}</view>
                  </el-text>
                </template>
                <view>地址：{{ row.address }}</view>
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column label="住户" prop="user" width="150">
            <template #default="{ row }">
              <view v-for="(user_info, user_index) in row.user" :key="user_index">
                {{ user_info.user_name }} <br/> {{ user_info.user_mobile }}
              </view>
            </template>
          </el-table-column>

          <el-table-column label="类型" prop="type_text" show-overflow-tooltip/>

          <el-table-column label="科室" prop="room_department" show-overflow-tooltip/>

          <!--                    <el-table-column label="添加时间" prop="add_time" show-overflow-tooltip />-->
          <!--                    <el-table-column label="总水电费" prop="device_total" show-overflow-tooltip>-->
          <!--                      <template #default="{ row }">-->
          <!--                        <view>{{ row.device_total }} 元</view> <br/>-->
          <!--                        <view>{{ row.device_fee_time }}</view>-->
          <!--                      </template>-->
          <!--                    </el-table-column>-->
          <el-table-column label="电梯费" prop="lift_fee" show-overflow-tooltip>
            <template #default="{ row }">
              <view>{{ row.lift_fee * 1 || '0.00' }} 元</view>
              <br/>
              <view>{{ row.lift_type_text }}</view>
            </template>
          </el-table-column>
          <el-table-column label="二次加压费" prop="second_pressure_fee" show-overflow-tooltip>
            <template #default="{ row }">
              <view>{{ row.second_pressure_fee * 1 || '0.00' }} 元</view>
              <br/>
            </template>
          </el-table-column>
          <!--<el-table-column label="排序" prop="sort" show-overflow-tooltip />-->

          <!--          <el-table-column label="状态" prop="status" width="100">-->
          <!--            <template #default="{ row }">-->
          <!--              <dict-value :options="dictData.show_status" :value="row.status"/>-->
          <!--            </template>-->
          <!--          </el-table-column>-->

          <el-table-column label="操作" fixed="right">
            <template #default="{ row }">
              <el-button
                  v-perms="['room.room_hospital/edit']"
                  type="primary"
                  link
                  @click="handleEdit(row)"
              >
                编辑
              </el-button>
              <el-button
                  v-perms="['room.room_hospital/delete']"
                  type="danger"
                  link
                  @click="handleDelete(row.id)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" :pager-extend="searchLists" :dict-data="dictData" @success="getLists"
                @close="showEdit = false"/>
  </div>
</template>

<script lang="ts" setup name="roomHospitalLists">

import {usePaging} from '@/hooks/usePaging'
import {useDictData} from '@/hooks/useDictOptions'
import {
  apiRoomHospitalLists,
  apiRoomHospitalDelete,
  apiRoomHospitalUpdateFee,
  apiRoomHospitalNewMonth,
  hospitalSearchLists
} from '@/api/room/room_hospital'
import {timeFormat} from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {View} from "@element-plus/icons-vue";
import {reactive} from "vue";

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)

const props1 = {
  checkStrictly: true
}

// 查询条件
const queryParams = reactive({
  cid: '',
  bid: '',
  name: '',
  address: '',
  status: '',
  type: '',
  add_time: '',
  device_total: '',
  device_fee_time: '',
  lift_type: '',
  start_time: '',
  end_time: '',
  cb_ids: '',
  room_department: '',
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({id}) => id)
}

// 获取字典数据
const {dictData} = useDictData('show_status')

// 分页相关
const {pager, getLists, resetParams, resetPage} = usePaging({
  fetchFun: apiRoomHospitalLists,
  params: queryParams
})

const searchLists = reactive({
  cb_lists: [],
  type_list: [],
  lift_list: [],
})
const getSearchLists = async () => {
  const lists = await hospitalSearchLists([])
  searchLists.cb_lists = lists.cb_lists
  searchLists.type_list = lists.type_list
  searchLists.lift_list = lists.lift_list
  return lists;
}
getSearchLists()

// 添加
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm('确定要删除？')
  await apiRoomHospitalDelete({id})
  getLists()
}

// 更新电梯房和二次加压费
const updateFee = async () => {
  await feedback.confirm('确定更新？')
  await apiRoomHospitalUpdateFee({})
  getLists()
}

// 月初始化
const monthInit = async () => {
  await feedback.confirm('确定月初始化？')
  await apiRoomHospitalNewMonth({})
  getLists()
}

getLists()
</script>

